package model

import (
	"gin-project/middleware"
	"gin-project/utils/errmsg"
	"gorm.io/gorm"
)

type Admin struct {
	gorm.Model
	UserName string
	PassWord string
	Pic      string
}

func (Admin) TableName() string {
	return "admin"
}

func AdminLogin(username, password string) (string,int) {
	var admin Admin
	err := db.Where("user_name = ?", username).First(&admin).Error
	if err != nil && gorm.ErrRecordNotFound != nil {
		return "", errmsg.USER_NAME_OR_PWD_FAILED
	}
	// 把加密密码和明文密码进行比较
	err = decrypt(admin.PassWord, password)
	if err != nil {
		return "", errmsg.USER_NAME_OR_PWD_FAILED
	}

	token, _ := middleware.GetToken(username)
	return token, errmsg.SUCCESS
}
